package com.star.uc.infras.config.webmvc;

import com.star.uc.infras.interceptor.CustomInterceptor;
import com.star.uc.infras.interceptor.SaTokenInterceptor;
import com.star.uc.infras.properties.SaTokenProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * satoken webmvc配置
 *
 * @author zhouhengzhe
 * @version 1.0
 * @date 2023/10/18 15:47
 */
@Configuration
public class SaTokenWebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private SaTokenProperties saTokenProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /**
         *  注册 SaTokenInterceptor 拦截器 登录拦截器
         */
        registry.addInterceptor(new SaTokenInterceptor())
                .addPathPatterns(saTokenProperties.getInterceptUrlList())
                .excludePathPatterns(saTokenProperties.getOpenUrlList())
                .order(1);
        /**
         * sa-token中的token续签的拦截器,30min内有请求就token续期，不操作就过期。
         */
        registry.addInterceptor(new CustomInterceptor())
                .addPathPatterns(saTokenProperties.getInterceptUrlList())
                .excludePathPatterns(saTokenProperties.getOpenUrlList())
                .order(2);

        /**
         * 签名拦截器，对参数验签
         */
        //registry.addInterceptor(new SignatureInterceptor())
        //        .addPathPatterns("/test/*")
        //        .order(3);
    }

    /**
     * 跨域配置
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("POST")
                .maxAge(3600)
                .exposedHeaders();
    }


    /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOriginPattern("*");
        // 允许任何域名使用
        config.addAllowedOrigin("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 有效期 1800秒
        config.setMaxAge(1800L);
        // 添加映射路径，拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        // 返回新的CorsFilter
        return new CorsFilter(source);
    }

    /**
     * 解决静态资源无法访问 ,针对swagger-ui
     *
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
